import * as THREE from "three"
import gsap from "gsap"
import vertex from "@/shader/lightWall/vertex.glsl"
import fragment from "@/shader/lightWall/fragment.glsl"

export default class LightWall {
  constructor(radius=2, length=4, position={x: 0, z: 0}, color=0xff0000) {
    this.geometry = new THREE.CylinderBufferGeometry( 
      radius, // radiusTop — 圆柱的顶部半径
      radius, // radiusBottom — 圆柱的底部半径
      2, // height — 圆柱的高度
      32, // radialSegments — 圆柱侧面周围的分段数
      1, // heightSegments — 圆柱侧面沿着其高度的分段数
      true, // openEnded — 指明该圆锥的底面是开放的还是封顶的
    );
		this.material = new THREE.ShaderMaterial( {
      vertexShader: vertex,
      fragmentShader: fragment,
      transparent: true,
      side: THREE.DoubleSide, // 双面可见
    } );
		this.mesh = new THREE.Mesh( this.geometry, this.material );
    this.mesh.position.set(position.x, 1, position.z)
    this.mesh.geometry.computeBoundingBox()
    // 获取物体的高度差
    let { max, min } = this.mesh.geometry.boundingBox
    let uHeight = max.y - min.y
    this.material.uniforms.uHeight = {
      value: uHeight
    }
    // 光墙动画
    gsap.to(this.mesh.scale, {
      x: length,
      z: length,
      duration: 1,
      repeat: -1,
      yoyo: true
    })
  }
  remove() {
    this.mesh.remove()
    this.mesh.removeFromParent()
    this.mesh.geometry.dispose()
    this.mesh.material.dispose()
  }
}